package com.lpx.demo.demo.config.listener;

import com.lpx.demo.demo.entity.BaseUserEntity;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.HashMap;
import java.util.Map;

@WebListener
public class SessionListener implements HttpSessionListener {
    public static Map<Long, HttpSession> sessionMap = new HashMap<>();

    /**
     * session销毁 监听
     *
     * @param hse
     */
    @Override
    public void sessionDestroyed(HttpSessionEvent hse) {
        HttpSession session = hse.getSession();
        this.DelSession(session);
    }

    public void DelSession(HttpSession session) {
        if (session != null) {
            // 删除单一登录中记录的变量
            if (session.getAttribute("user") != null) {
                BaseUserEntity user = (BaseUserEntity) session.getAttribute("user");
                SessionListener.sessionMap.remove(user.getId());
            }

        }
    }
}